package com.pw.study.structure.queue;

/**
 * @Desc: 自定义队列
 * @author: future
 * @since: 2022/10/23-21:23
 */
public class ArrayQue {
    private int front;
    private int rear;
    private final int[] arr;
    private final int maxSize;

    /**
     * 初始化队列
     */
    public ArrayQue(int maxSize) {
        this.maxSize = maxSize;
        front = -1;
        rear = -1;
        arr = new int[maxSize];
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        boolean resultBtn = false;
        if (rear == front) {
            resultBtn = true;
        }
        return resultBtn;
    }

    /**
     * 判断队列是否已满
     *
     * @return
     */
    public boolean isFull() {
        boolean resultBtn = false;
        if (rear == maxSize - 1) {
            resultBtn = true;
        }
        return resultBtn;
    }

    /**
     * 添加元素
     *
     * @param num
     */
    public void addArr(int num) {
        if (isFull()) {
            throw new RuntimeException("队列已满，不能添加元素了。。。");
        }
        arr[++rear] = num;
    }

    /**
     * 弹出元素
     */
    public int getOne() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能获取元素了");
        }
        return arr[++front];
    }

    /**
     * 显示队列元素
     */
    public void showAll() {
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能获取到元素。。。。");
        }
        return arr[front + 1];
    }

}
